﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Torch_Services
{
    public class BooksCategoryService : IBooksCategoryService
    {
        private IBooksCategoryDal _booksCy;
        private IMapper _mapper;
        public BooksCategoryService(IBooksCategoryDal booksCy, IMapper mapper)
        {
            _booksCy = booksCy;
            _mapper = mapper;
        }

        public async Task<int> AddBooksCategoryAsync(string bookType, int parentId)
        {
            return await _booksCy.AddAsync(new Bookscategory 
            {
                BooksType = bookType,
                ParentId = parentId
            });
        }

        public async Task<int> DeleteBooksCategoryAsync(int id)
        {
            var data = await _booksCy.QueryAsync(id);
            return data == null ? -1 : await _booksCy.DeleteAsync(data);
        }

        public async Task<int> EditBooksCategoryAsync(int id, string bookType, int parentId)
        {
            var data = await _booksCy.QueryAsync(id);
            if (data == null)
                return -1;
            data.BooksType = bookType;
            data.ParentId = parentId;
            return await _booksCy.EditAsync(data);
        }

        public async Task<List<BooksCategoryDto>> QueryBooksCategoryAllAsync()
        {
            var data = await _booksCy.QueryAsync();
            return _mapper.Map<List<Bookscategory>,List<BooksCategoryDto>>(data);
        }

        public async Task<BooksCategoryDto> QueryBooksCategoryByIdAsync(int id)
        {
            var data = await _booksCy.QueryAsync(id);
            return _mapper.Map<Bookscategory, BooksCategoryDto>(data);
        }

        public async Task<List<BooksCategoryDto>> QueryBooksCategoryByTypeNameAsync(string booksType)
        {
            var data = await _booksCy.QueryAsync(bkc => bkc.BooksType.Contains(booksType));
            return _mapper.Map<List<Bookscategory>, List<BooksCategoryDto>>(data);
        }
    }
}
